package com.xplay.xpocker.room.message;

import com.xplay.xpocker.util.JacksonStringUtil;

import java.util.HashMap;

/**
 * 消息发送给客户端
 * 涉及到以下行为
 * <p>
 * 原始数据备份
 * 1. 过滤
 * 2. 转换
 */

public class MessageToClient extends HashMap<String, Object> {

    @Override
    public MessageToClient clone() {
        MessageToClient clone = new MessageToClient();
        clone.putAll(this);
        return clone;
    }



    public void multiRemove(String... keys) {
        for (String key : keys) {
            this.remove(key);
        }
    }
    public void multi(Object... objects) {
        for (int i = 0, j = objects.length; i < j; i += 2) {
            if (objects.length < i + 1) {
                break;
            }
            String key = null;
            if (objects[i] instanceof String) {
                key = objects[i].toString();
            } else {
                key = JacksonStringUtil.objectToString(objects[i]);
            }
            if (key == null) {
                continue;
            }
            put(key, objects[i + 1]);
        }
    }

    public static Builder builder() {
        return new Builder(new MessageToClient());
    }

    public static class Builder {
        private MessageToClient messageAction;

        public Builder(MessageToClient messageAction) {
            this.messageAction = messageAction;
        }

        public Builder multi(Object... objects) {
            messageAction.multi(objects);
            return this;
        }

        public MessageToClient build() {
            return messageAction;
        }
    }

    public static void main(String[] args) {
        System.out.println(JacksonStringUtil.objectToString(MessageToClient.builder().multi("aaa", "fff").build()));
    }
}
